<?php
/**
 * 描述
 * Created by Paco
 * Date: 2019/1/29 Time: 16:06
 */

namespace App\Repositories\Shop;


use App\Models\Log\LogShop;
use App\Models\Shop;
use App\Models\User\User;
use App\Repositories\Repository;
use Illuminate\Support\Facades\DB;

class ShopRepository extends Repository
{
    public function show()
    {
        $seller = $this->getUser();
        if (!$seller->shop_id) {
            return $this->respondError(10900);
        }
        $shop = Shop::query()->select(['shop_name',
            'busniess_license',
            'trademark',
            'environment_protection',
            'moblie',
            'company',
            'contact',
            'tel'
        ])->find($seller->shop_id);

        return $this->respondWith($shop);
    }

    public function store($data)
    {
        $seller = $this->getUser();
        if ($seller->shop_id) {
            return $this->respondError(10901);
        }
        DB::beginTransaction();
        try {
            if (!is_array($data['busniess_license'])) {
                $data['busniess_license'] = explode(',', $data['busniess_license']);
            }
            if (!is_array($data['trademark'])) {
                $data['trademark'] = explode(',', $data['trademark']);
            }

            if (!is_array($data['environment_protection'])) {
                $data['environment_protection'] = explode(',', $data['environment_protection']);
            }

            $shop = Shop::query()->create($data);
            $data['shop_id'] = $shop->shop_id;
            LogShop::query()->create($data);
            $user = User::query()->find($seller->user_id);
            $user->shop_id = $shop->shop_id;
            $user->save();
            DB::commit();
            $seller->shop_id = $shop->shop_id;
            $token = $this->getToken();
            $data = [
                'user_id' => $user->user_id,
                'nick_name' => $user->nick_name,
                'avatar' => $user->avatar,
                'indentity_id' => $seller->indentity_id,
                'indentity_type' => $seller->indentity_type,
                'pid' => $seller->pid,
                'openid' => $seller->openid,
                'shop_id' => $shop->shop_id
            ];
            app('phpredis')->setex('token_' . $token, 86400, json_encode($data));
            return $this->respondSuccess();
        } catch (\Exception $e) {
            DB::rollBack();
            $this->throwRespondWithMessage($e->getMessage());
            return $this->respondError(10110);
        }
    }

    public function update($params)
    {
        $params['busniess_license'] = json_encode($params['busniess_license']);
        $params['trademark'] = json_encode($params['trademark']);
        $params['environment_protection'] = json_encode($params['environment_protection']);
        $params['checked'] = 0;
        $shop = LogShop::query()->find($params['shop_id']);
        if (!$shop) {
            return $this->respondError(10109);
        }
        try {
            $shop->fill($params)->save();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->respondError(10108);
        }
    }
}